package com.yc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.mapper.AddressMapper;
import com.yc.model.Address;
import com.yc.service.AddressService;
import com.yc.web.api.IdGeneratorApi;
import com.yc.web.bean.AddressDTO;
import com.yc.web.bean.ResponseResult;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
@Log
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    // 状态常量
    private static final int ADDRESS_STATUS_NORMAL = 1; // 正常状态
    private static final int ADDRESS_STATUS_DELETED = 0; // 已删除状态
    private static final int IS_DEFAULT = 1; // 默认地址
    private static final int NOT_DEFAULT = 0; // 非默认地址

    private final IdGeneratorApi idGeneratorApi;
    /**
     * 查看地址列表
     * @param userId
     * @return
     */
    @Override
    public List<AddressDTO> listAddress(Long userId) {
        if(userId == null){
            throw new IllegalArgumentException("userId不能为空");
        }
        LambdaQueryWrapper<Address> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Address::getUserId,userId)
                .eq(Address::getAddressStatus,ADDRESS_STATUS_NORMAL);
        List<Address> addresses = this.list(queryWrapper);

        List<AddressDTO> addressDTOs = addresses.stream().map(address -> {
            AddressDTO addressDTO = new AddressDTO();
            BeanUtils.copyProperties(address,addressDTO);
            return addressDTO;
        }).collect(Collectors.toList());
        return addressDTOs;
    }

    /**
     * 添加地址
     * @param userId
     * @param addressDTO
     * @return
     */
    @Override
    @Transactional
    public AddressDTO addAddress(Long userId, AddressDTO addressDTO) {
        if(userId == null){
            throw new IllegalArgumentException("userId不能为空");
        }
        if(addressDTO == null){
            throw new IllegalArgumentException("addressDTO不能为空");
        }
        addressDTO.setUserId(userId);
        ResponseResult responseResult = idGeneratorApi.getNextId();
        addressDTO.setAddressId(Long.valueOf(responseResult.getData().toString()));
        Address address = new Address();
        BeanUtils.copyProperties(addressDTO,address);
        boolean success=this.save(address);
        if(success){
            return addressDTO;
        }
        throw new RuntimeException("添加地址失败");
    }

    /**
     * 更新地址
     * @param userId
     * @param addressDTO
     */
    @Override
    public void updateAddress(Long addressId, Long userId, AddressDTO addressDTO) {
        if(userId == null || addressId == null || addressDTO == null){
            throw new IllegalArgumentException("userId或addressId或addressDTO不能为空");
        }
        Address address = this.getById(addressId);
        if(address == null){
            throw new IllegalArgumentException("地址不存在");
        }
        if(!address.getUserId().equals(userId)){
            throw new IllegalArgumentException("用户id不匹配");
        }
        BeanUtils.copyProperties(addressDTO,address);
        boolean success = this.updateById(address);
        if(!success){
            throw new RuntimeException("更新地址失败");
        }
    }

    /**
     * 软删除地址
     * @param addressId
     */
    @Override
    public void softDeleteAddress(Long addressId) {
        Address address = this.getById(addressId);
        if(address == null){
            throw new IllegalArgumentException("地址不存在");
        }
        address.setAddressStatus(ADDRESS_STATUS_DELETED);
        boolean success = this.updateById(address);
        if(!success){
            throw new RuntimeException("删除地址失败");
        }
    }

    /**
     * 设置默认地址
     * @param userId
     * @param addressId
     */
    @Override
    public void setDefaultAddress(Long userId, Long addressId) {
        if(userId == null || addressId == null){
            throw new IllegalArgumentException("userId或addressId不能为空");
        }
        Address address = this.getById(addressId);
        if(address == null){
            throw new IllegalArgumentException("地址不存在");
        }
        if(!address.getUserId().equals(userId)){
            throw new IllegalArgumentException("用户id不匹配");
        }
        address.setIsDefault(IS_DEFAULT);
        boolean success = this.updateById(address);
        if(!success){
            throw new RuntimeException("设置默认地址失败");
        }
    }

}
